package com.hzhq.contract.controller;

import com.hzhq.contract.bean.dto.ContractTemplateDTO;
import com.hzhq.contract.bean.dto.UserDTO;
import com.hzhq.contract.bean.entity.Company;
import com.hzhq.contract.bean.entity.ContractTemplate;
import com.hzhq.contract.bean.entity.User;
import com.hzhq.contract.bean.vo.ContractTemplateVO;
import com.hzhq.contract.convert.ContractTemplateConvertBasic;
import com.hzhq.contract.result.Result;
import com.hzhq.contract.service.TemplateService;
import com.hzhq.contract.service.UserService;
import com.hzhq.contract.status.DefaultValue;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.web.bind.annotation.*;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Optional;


/**
 * @author hzhq
 * @create 2021/4/9 11:30 上午
 * @email hzhq1255@163.com
 */
@RestController
@CrossOrigin
@Slf4j
@Api(tags = "模板管理")
@RequestMapping("/api/v1/templates")
@ResponseBody
public class TemplateController {

    @Autowired
    private TemplateService templateService;

    @Autowired
    private UserService userService;





    @ApiOperation(value = "保存模板")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "contractTemplateVO",value = "模板",paramType = "body",dataTypeClass = ContractTemplateVO.class, dataType = "ContractTemplateVO"),
    })
    @RequestMapping(value = "/save",method = RequestMethod.POST)
    private Result<Object> saveTemplate( @RequestBody ContractTemplateDTO templateDTO){
        return templateService.saveTemplate(templateDTO);
    }

    @ApiOperation("搜索模板")
    @RequestMapping(value = "/search/query/currentPage/{currentPage}/pageSize/{pageSize}",method = RequestMethod.GET)
    private Result<Object> searchAllPageByQuery(@PathVariable("currentPage") @NotNull Integer currentPage,
                                                @PathVariable("pageSize") @NotNull Integer pageSize,
                                                @RequestParam(value = "keyword",required = false) String keyword,
                                                @RequestParam(value = "createUser",required = false) String createUser){
        final Query query = new Query();
        final Criteria criteria = new Criteria();
        List<Criteria> ands = new ArrayList<>();
        List<Criteria> ors = new ArrayList<>();
        if (createUser != null){
            UserDTO userDTO = userService.getOneByName(createUser);
            if (userDTO == null){
                return Result.errorWithMsg("创建者不存在");
            }
            ors.add(Criteria.where(ContractTemplate.CREATE_USER_FIELD+".$id").is(userDTO.getUserId()));
        }
        if (keyword != null){
            ors.add(Criteria.where(ContractTemplate.TEMPLATE_NAME_FIELD).regex("^.*"+keyword+".*$"));
        }
        if (ors.size() != 0){
            criteria.orOperator(ors.toArray(Criteria[]::new));
        }
        ands.add(Criteria.where(Company.DELETED_FIELD).is(DefaultValue.NOT_DELETED));
        criteria.andOperator(ands.toArray(Criteria[]::new));
        query.addCriteria(criteria);
        return templateService.searchAllPageByQuery(query,currentPage,pageSize);
    }

    @ApiOperation(value = "删除模板")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "templateId",value = "模板id",paramType = "path",dataTypeClass = String.class, dataType = "String")
    })
    @RequestMapping(value = "/delete/{templateId}", method = RequestMethod.POST)
    private Result<Object> deleteTemplate(@PathVariable("templateId") @NotNull String templateId){

        return templateService.deleteTemplate(templateId);
    }


    @ApiOperation(value = "用户自己添加的模板")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username",value = "当前用户",paramType = "path",dataTypeClass = String.class, dataType = "String"),
            @ApiImplicitParam(name = "currentPage",value = "当前页",paramType = "path",dataTypeClass = Integer.class, dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize",value = "每页个数",paramType = "path",dataTypeClass = Integer.class, dataType = "Integer")
    })
    @RequestMapping(value = "/user/{username}/currentPage/{currentPage}/pageSize/{pageSize}", method = RequestMethod.GET)
    private Result<Object> getPageByApplicant(@PathVariable("username") @NotNull String username,
                                              @PathVariable("currentPage") @NotNull Integer currentPage,
                                              @PathVariable("pageSize") @NotNull Integer pageSize){

        return templateService.getPageByApplicant(username,currentPage,pageSize);
    }


    @ApiOperation(value = "获取所有模板")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "currentPage",value = "当前页",paramType = "path",dataTypeClass = Integer.class, dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize",value = "每页个数",paramType = "path",dataTypeClass = Integer.class, dataType = "Integer")
    })
    @RequestMapping(value = "/currentPage/{currentPage}/pageSize/{pageSize}", method = RequestMethod.GET)
    private Result<Object> getPage(@PathVariable("currentPage") @NotNull Integer currentPage,
                                              @PathVariable("pageSize") @NotNull Integer pageSize){

        return templateService.getPage(currentPage,pageSize);
    }

    @ApiOperation(value = "获取所有模板list")
    @RequestMapping(method = RequestMethod.GET)
    private Result<Object> getList(){
        return templateService.getList();
    }



    @ApiOperation(value = "获取部门所有模板")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "depart",value = "部门",paramType = "path",dataTypeClass = String.class, dataType = "String"),
            @ApiImplicitParam(name = "currentPage",value = "当前页",paramType = "path",dataTypeClass = Integer.class, dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize",value = "每页个数",paramType = "path",dataTypeClass = Integer.class, dataType = "Integer")
    })
    @RequestMapping(value = "/depart/{depart}/currentPage/{currentPage}/pageSize/{pageSize}", method = RequestMethod.GET)
    private Result<Object> getPageByDepart(@PathVariable("depart") @NotNull String depart,
                                           @PathVariable("currentPage") @NotNull Integer currentPage,
                                           @PathVariable("pageSize") @NotNull Integer pageSize){
        return templateService.getPageByDepart(depart,currentPage,pageSize);
    }


    @ApiOperation(value = "通过id获取模板")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "templateId",value = "模板id",paramType = "path",dataTypeClass = String.class, dataType = "String"),
    })
    @RequestMapping(value = "/templateId/{templateId}", method = RequestMethod.GET)
    private Result<Object> getOneByTemplateId(@PathVariable("templateId") String templateId){

        return templateService.getOneByTemplateId(templateId);
    }


}
